import 'package:flutter/material.dart';
import '../error_page.dart';
import '../login_page.dart';
import '../modules/calendar/ui/calendar_page.dart';
import '../modules/home/ui/home_page.dart';
import '../modules/pop/ui/pop_page.dart';
import '../modules/refresher/ui/refresher_page.dart';
import '../modules/search/views/search_page.dart';
import '../settings_page.dart';
import 'routes.dart';

class AppRouter {
  static Route<dynamic> generateRoute(RouteSettings settings) {
    // 使用 switch case 处理不同的路由名
    switch (settings.name) {
      case Routes.home:
        return CustomPagePeterRoute<dynamic>(
          builder: (_) => const HomePage(),
          settings: settings, // Pass settings
        );

      case Routes.calendar:
        return CustomPagePeterRoute<dynamic>(
          builder: (_) => const CalendarPage(),
          settings: settings, // Pass settings
        );

      case Routes.pop:
        return CustomPagePeterRoute<dynamic>(
          builder: (_) => const PopPage(),
          settings: settings, // Pass settings
        );

      case Routes.login:
        return CustomPagePeterRoute<dynamic>(
          builder: (_) => const LoginPage(),
          settings: settings, // Pass settings
        );
      case Routes.refresher:
        return CustomPagePeterRoute<dynamic>(
          builder: (_) => const RefresherPage(),
          settings: settings, // Pass settings
        );
      case Routes.search:
        return CustomPagePeterRoute<dynamic>(
          builder: (_) => const SearchPage(),
          settings: settings, // Pass settings
        );
      case Routes.setting:
        return CustomPagePeterRoute<dynamic>(
          builder: (_) => const SettingsPage(),
          settings: settings, // Pass settings
        );

      default:
        return CustomPagePeterRoute<String>(
          builder: (_) => const ErrorPage(),
          settings: settings, // Pass settings
        );
    }
  }
  static Route<dynamic> unknownRoute(RouteSettings settings) {
    return CustomPagePeterRoute(
      builder: (_) => const ErrorPage(message: "Unknown route",),
      settings: settings,
    );
  }
}

/// 自定义路由类 实现滑动动画 替换默认的MaterialPageRoute
class CustomPagePeterRoute<T> extends PageRouteBuilder<T> {
  final WidgetBuilder builder;
  @override
  final RouteSettings settings;

  CustomPagePeterRoute({required this.builder, required this.settings})
    : super(
        settings: settings,
        pageBuilder:
            (context, animation, secondaryAnimation) => builder(context),
        transitionsBuilder: (context, animation, secondaryAnimation, child) {
          const begin = Offset(1.0, 0.0); // 新页面从右侧开始
          const end = Offset.zero; // 滑动到屏幕中央
          const curve = Curves.easeInOut; // 缓入缓出曲线

          var tween = Tween(
            begin: begin,
            end: end,
          ).chain(CurveTween(curve: curve));
          var offsetAnimation = animation.drive(tween);

          return SlideTransition(position: offsetAnimation, child: child);
        },
        transitionDuration: const Duration(milliseconds: 300), // 动画时长 300ms
      );
}
